// $Id: stream.h 73 2011-04-24 05:41:54Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2011 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef STREAM_H_
#define STREAM_H_

#include <sys/queue.h>
#include <sys/tree.h>

#include "client.h"
#include "daemon.h"

struct niohttpd_stream {
	const char *uri;
	TAILQ_ENTRY(niohttpd_stream) next;
};

TAILQ_HEAD(niohttpd_stream_list, niohttpd_stream);

struct niohttpd_stream_entry {
	char *uri;
	unsigned int goplen;
	struct niohttpd_stream stream;
	struct niohttp_headers *headers;
	struct niohttpd_clients clients;
	niohttpd_stream_cb cb;
	void *cbarg;
	struct {
		enum niohttpd_stream_qos_direction dir;
		niohttpd_stream_qos_cb cb;
		void *cbarg;
	} qos;
	RB_ENTRY(niohttpd_stream_entry) entry;
};

RB_HEAD(niohttpd_stream_tree, niohttpd_stream_entry);

int niohttpd_stream_entry_compare(
		struct niohttpd_stream_entry *a, struct niohttpd_stream_entry *b);

RB_PROTOTYPE(niohttpd_stream_tree, niohttpd_stream_entry, next,
		niohttpd_stream_entry_compare);

struct niohttpd_streams {
	struct niohttpd_stream_list list;
	struct niohttpd_stream_tree tree;
};

struct niohttpd_streams *niohttpd_streams_new(void);

void niohttpd_streams_free(struct niohttpd_streams *servlets);

int niohttpd_streams_add(struct niohttpd_streams *servlets,
		const char *uri, struct niohttpd_stream_entry **out);

void niohttpd_streams_remove(struct niohttpd_streams *servlets,
		struct niohttpd_stream_entry *entry);

struct niohttpd_stream_entry *niohttpd_streams_find(
		struct niohttpd_streams *servlets, const char *uri);

void niohttpd_streams_clear(struct niohttpd_streams *servlets);

#endif /* STREAM_H_ */
